package cn.itsource.order.listener;

import cn.itsource.order.domain.OrderPetAcquisition;
import cn.itsource.order.mapper.OrderPetAcquisitionMapper;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RocketMQMessageListener(topic = "pet_delay_topic", consumerGroup = "consumer-group")
public class ScheuleListener implements RocketMQListener<String> {
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;
    @Override
    public void onMessage(String s) {
        // 根据订单号查询订单
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadByOrderSn(s);
        // 判断订单状态，若为0 - 未处理 - 取消订单
        if (order!=null && order.getState() == 0){
            // 1.将寻主信息的shopId置空
            searchMasterMsgMapper.reject(order.getSearchMasterMsgId());
            // 2.删除宠物信息 pet之前录入过，但没有接回来，所以删除
            petMapper.remove(order.getPetId());
            // 3.修改订单的状态
            order.setState(-1); // 取消状态
            orderPetAcquisitionMapper.update(order);
            System.out.println("由于您未确认订单，订单号为：" + s + "的订单已被自动取消！");
        }
    }
}